package io.kiki.stack.http.feign.form.multipart;

import feign.codec.EncodeException;
import lombok.val;

import static io.kiki.stack.http.feign.form.ContentProcessor.CRLF;

/**
 * A writer for a single parameter.
 *
 * @author Artem Labazin
 */
public class SingleParameterWriter extends AbstractWriter {

    @Override
    public boolean isApplicable(Object value) {
        return value instanceof Number || value instanceof CharSequence || value instanceof Boolean;
    }

    @Override
    protected void write(Output output, String key, Object value) throws EncodeException {
        val string = new StringBuilder().append("Content-Disposition: form-data; name=\"").append(key).append('"').append(CRLF).append("Content-Type: text/plain; charset=").append(output.getCharset().name()).append(CRLF).append(CRLF).append(value.toString()).toString();

        output.write(string);
    }
}
